//
//  YTOrderManagerViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/6/2.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTOrderManagerViewModel.h"
#import "YTOrderServices.h"
#import "YTOrderServiceCityModel.h"
#import "YTPickedOrderModel.h"

@implementation YTOrderManagerViewModel

- (void)initialize {
    self.pickedDataArray = [NSMutableArray array];
    self.serviceCitys = [NSMutableArray array];
    self.tabTitleArray = [NSArray array];
    if (isDriverTeam) {
        _tabTitleArray = @[@"全部", @"待指派", @"待联系", @"待服务", @"被取消", @"待结算"];
    } else {
        _tabTitleArray = @[@"全部", @"待联系", @"待服务", @"被取消", @"待结算"];
    }
    
    [self changeTabClass];
    [self loadData];
    [self filterData];
}

#pragma mark - tab切换

- (void)changeTabClass {
    @weakify(self);
    self.changeTabCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber *index) {
        @strongify(self);
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [self loadDataWithSubscriber:subscriber];
            return nil;
        }] initially:^{
            [self cleanData];
            [self changeTabValue:index.integerValue];
        }];
    }];
}


/**
 订单类型，0全部订单，1待联系，2服务中，3待结算、已完成，4已结算、已完成，5退款中，501待付款、付款中，502被取消，6待指派
 在个人司导的页面上，少了待指派这个tab，所以要做类型判定
 上面是关于订单的tab值，本来是设计好的，然而产品那边要求改掉，现在就变成这种不三不四
 所以才有了这段逻辑说明,是不是很震惊？是不是很傻逼？🤷‍♀️

 @param value tab值
 */
- (void)changeTabValue:(NSInteger)value {
    if (isDriverTeam) {
        switch (value) {
            case 0: // 全部订单
                self.tabClass = value;
                break;
            case 1: // 待指派
                self.tabClass = 6;
                break;
            case 2: // 待联系
                self.tabClass = 1;
                break;
            case 3: // 待服务
                self.tabClass = 2;
                break;
            case 4: // 被取消
                self.tabClass = 502;
                break;
            case 5: // 待结算
                self.tabClass = 3;
                break;
        }
    }
    
    if (isDriver) {
        switch (value) {
            case 0: // 全部, 待联系, 待服务
            case 1:
            case 2:
                self.tabClass = value;
                break;
            case 3: // 被取消
                self.tabClass = 502;
                break;
            case 4: // 待结算
                self.tabClass = 3;
                break;
        }
    }
}

#pragma mark - 刷新和加载数据

- (void)loadData {
    // 刷新
    @weakify(self);
    self.requestRemoteDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *input) {
        @strongify(self);
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [self loadDataWithSubscriber:subscriber];
            return nil;
        }] initially:^{
            [self cleanData];
        }];
    }];
    // 加载更多
    self.loadMoreDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *input) {
        @strongify(self);
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [self loadDataWithSubscriber:subscriber];
            return nil;
        }] initially:^{
            self.page++;
        }];
    }];
}

#pragma mark - 筛选

- (void)filterData {
    @weakify(self);
    self.filterCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *filterDict) {
        @strongify(self);
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [self loadDataWithSubscriber:subscriber];
            return nil;
        }] initially:^{
            [self cleanData];
            self.seatType = filterDict[@"seatType"];
            self.subType = filterDict[@"subType"];
            self.serviceCity = filterDict[@"serviceCity"];
            self.useTimeBegin = filterDict[@"useTimeBegin"];
            self.useTimeEnd = filterDict[@"useTimeEnd"];
        }];
    }];
}

#pragma mark - Private

- (void)loadDataWithSubscriber:(id<RACSubscriber>)subscriber {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"page"] = [NSString stringWithFormat:@"%zd", self.page];
    dict[@"orderType"] = [NSString stringWithFormat:@"%zd", self.tabClass];
    dict[@"seatType"] = self.seatType;
    dict[@"subType"] = self.subType;
    dict[@"serviceCity"] = self.serviceCity;
    dict[@"useTimeBegin"] = self.useTimeBegin;
    dict[@"useTimeEnd"] = self.useTimeEnd;
    [[YTOrderServices loadTeamOrderList:dict] subscribeNext:^(NSDictionary *response) {
        NSDictionary *data = response[@"responseData"];
        NSArray<YTPickedOrderModel *> *dataArray = [NSArray yy_modelArrayWithClass:[YTPickedOrderModel class] json:data[@"orderList"]];
        [self.pickedDataArray addObjectsFromArray:dataArray];
        self.serviceCitys = [[NSArray yy_modelArrayWithClass:[YTOrderServiceCityModel class] json:data[@"serviceCity"]] mutableCopy];
        [subscriber sendNext:dataArray];
        [subscriber sendCompleted];
    }];
}

- (void)cleanData {
    self.page = 1;
    self.seatType = nil;
    self.subType = nil;
    self.serviceCity = nil;
    self.useTimeEnd = nil;
    self.useTimeEnd = nil;
    [self.pickedDataArray removeAllObjects];
    [self.serviceCitys removeAllObjects];
}
@end
